package com.store.app.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.store.app.BaseActivity;
import com.store.app.ExitApplication;
import com.store.app.R;
import com.store.app.adapter.CouponGoCashAdapter;
import com.store.app.bean.CouponGoCashBean;
import com.store.app.net.NetRequest;
import com.store.app.net.ifc.IOnHttpListener;
import com.store.app.utils.MoneyUtil;
import com.store.app.utils.Utils;

import org.json.JSONException;
import org.json.JSONObject;

import java.math.BigDecimal;

import de.greenrobot.event.EventBus;

/**
 * 零钱充值页面
 */
public class CouponGoCashActivity extends BaseActivity  implements IOnHttpListener {
	private TextView title;
	private NetRequest netRequest;
	private EditText et_phone;
	private EditText et_money;
	//private GridView gridview;
	private TextView submit;
	private volatile ProgressDialog mProgressDialog;
	private String phone,money;
	private CouponGoCashAdapter mAdapter;
	//private String[] data={"50","100","200"};
	//private int s_position=-1;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ExitApplication.getInstance().addActivity(this);
		setContentView(R.layout.activity_coupongocash);
		initView();
		EventBus.getDefault().register(this);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		EventBus.getDefault().unregister(this);
	}

	private void initView() {
		netRequest = new NetRequest(this);
		netRequest.requestVerifyUserToken(2);
		et_phone = (EditText) findViewById(R.id.et_phone);
		et_money = (EditText) findViewById(R.id.et_money);
		submit = (TextView) findViewById(R.id.submit);
		//gridview= (GridView) findViewById(R.id.gridview);
		//mAdapter = new CouponGoCashAdapter(CouponGoCashActivity.this,data);
		//gridview.setAdapter(mAdapter);
		submit.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				submit();
			}
		});
		title=(TextView) findViewById(R.id.tvTitle);
		title.setText("零钱充值");
		findViewById(R.id.public_ll_return).setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
//		gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//			@Override
//			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//				s_position=position;
//				mAdapter.setCount(position);
//				mAdapter.notifyDataSetChanged();
//			}
//		});
	}
	private void submit() {
		phone = et_phone.getText().toString().trim();
		money=et_money.getText().toString().trim();
		if (TextUtils.isEmpty(phone)) {
			Toast.makeText(this, "手机号不能为空", Toast.LENGTH_SHORT).show();
			return;
		}
		if(!Utils.matchPhone(phone)){
			Toast.makeText(this, "手机号格式不对!", Toast.LENGTH_SHORT).show();
			return;
		}
		if (TextUtils.isEmpty(money)) {
			Toast.makeText(this, "请输入充值金额", Toast.LENGTH_SHORT).show();
			return;
		}
		money = MoneyUtil.formatMoney(money);
		if (moneyComp(money,"200")){
			Toast.makeText(this, "请输入小于或等于200元金额", Toast.LENGTH_SHORT).show();
			return;
		}
		if(!moneyComp(money,"0")){
			Toast.makeText(this, "输入金额需大于0", Toast.LENGTH_SHORT).show();
			return;
		}
		showProgressDialog("", "手机号验证中。。。");
		netRequest.requestQueryConsumerMememberId(1, phone);

	}
	@Override
	public void finish() {
		super.finish();
		try {
			((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
					.hideSoftInputFromWindow(getCurrentFocus()
									.getWindowToken(),
							InputMethodManager.HIDE_NOT_ALWAYS);
		}catch (Exception e){
		}
	}

	@Override
	public void onAfinalFail() {
		dismissProgressDialog();
	}

	@Override
	public void onExecuteSuccess(int requestId, String result, String msg) {
		dismissProgressDialog();
		if(requestId==1){
			JSONObject json = null;
			try {
				json = new JSONObject(result);
				String memberId = json.getString("member_id");
				Intent intent=new Intent(CouponGoCashActivity.this,CashPaymentActivity.class);
				intent.putExtra("phone",phone);
				intent.putExtra("money",money);
				intent.putExtra("member_id",memberId);
				startActivity(intent);
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public void onExecuteFail(int requestId, String msg) {
		dismissProgressDialog();
		Toast.makeText(this,msg,Toast.LENGTH_SHORT).show();
	}
	protected void dismissProgressDialog() {
		try {
			if (mProgressDialog != null)
				mProgressDialog.dismiss();
		}catch (Exception e){

		}
	}
	protected void showProgressDialog(String title, String msg) {
		if (mProgressDialog == null) {
			mProgressDialog = new ProgressDialog(this);
		}
		if(!TextUtils.isEmpty(title)){
			mProgressDialog.setTitle(title);}
		mProgressDialog.setMessage(msg);
		mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		mProgressDialog.setCancelable(false);
		mProgressDialog.show();
	}


	public void onEvent(CouponGoCashBean bean) {
		if(bean.type==1){
			finish();
		}
	}
	/**
	 * 值比较大小
	 * <br/>如果valueStr大于等于compValueStr,则返回true,否则返回false
	 *  true 代表可用余额不足
	 * @param valueStr (需要消费金额)
	 * @param compValueStr (可使用金额)
	 * @return
	 */
	public  boolean moneyComp(String valueStr,String compValueStr){
		BigDecimal value = new BigDecimal(valueStr);
		BigDecimal compValue = new BigDecimal(compValueStr);
		//0:等于    >0:大于    <0:小于
		int result = value.compareTo(compValue);
		if(result > 0){
			return true;
		}else{
			return false;
		}
	}
}